﻿Imports HMSLibrary
Imports HMSDataLayer
Imports System.Collections.ObjectModel

Public Class PatientVisitUserControl
    Implements IHMSData

    Public Property ActionMode As HMSLibrary.FormAction Implements HMSLibrary.IHMSData.ActionMode
    Dim ProblemList As ObservableCollection(Of PatientProblem)
    Dim findingList As ObservableCollection(Of PatientExamFinding)
    Dim DiagnosisList As ObservableCollection(Of PatientDiagnosis)
    Dim TreatmentList As ObservableCollection(Of PatientPrescription)

    Public Sub AddNew() Implements HMSLibrary.IHMSData.AddNew
        ChiefComplaintsComboBox.SelectedIndex = -1
        NotesTextBox.Text = String.Empty
        VisitDateDatePicker.SelectedDate = Date.Now
        VisitDateDatePicker.DisplayDateEnd = Date.Now
        ActionMode = FormAction.ADD

        ProblemList = New ObservableCollection(Of PatientProblem)()
        findingList = New ObservableCollection(Of PatientExamFinding)()
        DiagnosisList = New ObservableCollection(Of PatientDiagnosis)
        TreatmentList = New ObservableCollection(Of PatientPrescription)
        ProblemsDataGrid.ItemsSource = ProblemList
        FindingsDataGrid.ItemsSource = findingList
        DiagnosisDataGrid.ItemsSource = DiagnosisList
        TreatmentsDataGrid.ItemsSource = TreatmentList
        AddDiagnosisButton.IsEnabled = True
        AddFindingsButton.IsEnabled = True
        AddProblemsButton.IsEnabled = True
        AddTreatmentButton.IsEnabled = True
        PrintPrescriptionButton.IsEnabled = False
    End Sub

    Public Sub BackView() Implements HMSLibrary.IHMSData.BackView

    End Sub

    Public Function DataValidation() As Boolean Implements HMSLibrary.IHMSData.DataValidation
        If String.IsNullOrEmpty(ConsultingDocComboBox.Text) Then
            CommonMessages.ShowValidationMsg("Please select doctor name!")
            Return False
        End If
        If String.IsNullOrEmpty(ChiefComplaintsComboBox.Text) Then
            CommonMessages.ShowValidationMsg("Please select Chief Complaints!")
            Return False
        End If
        Return True
    End Function

    Public Sub FindData() Implements HMSLibrary.IHMSData.FindData

    End Sub

    Public Property ID As Integer Implements HMSLibrary.IHMSData.ID

    Public Sub LoadData(ByVal ID As Integer) Implements HMSLibrary.IHMSData.LoadData

    End Sub

    Public Function SaveData() As Boolean Implements HMSLibrary.IHMSData.SaveData
        If DataValidation() = False Then Return False
        Dim dataLayer As New PatientDataLayer()
        Dim visitObj As New PatientVisit
        visitObj.patientID = ID
        visitObj.visitID = Val(IDTextBlock.Text)
        visitObj.visitNote = NotesTextBox.Text.Trim
        visitObj.visitDataTime = VisitDateDatePicker.SelectedDate
        visitObj.compalinedComplaints = ChiefComplaintsComboBox.SelectedValue
        visitObj.consultingDoc = ConsultingDocComboBox.SelectedValue
        visitObj.ProblemList = ProblemList
        visitObj.findingList = findingList
        visitObj.DiagnosisList = DiagnosisList
        visitObj.TreatmentList = TreatmentList
        If dataLayer.SavePatientVisits(visitObj, ActionMode) = True Then
            PrintPrescriptionButton.IsEnabled = True
            Return True
        End If
    End Function

    Public Sub New()

        ' This call is required by the designer.
        InitializeComponent()

        ' Add any initialization after the InitializeComponent() call.

    End Sub

    Private Sub AddDiagnosisButton_Click(ByVal sender As Object, ByVal e As System.Windows.RoutedEventArgs) Handles AddDiagnosisButton.Click
        Dim win As New PatientDiagnosisWindow
        win.ShowDialog()
        If win.DiagnosisList Is Nothing Then Exit Sub
        For Each f In win.DiagnosisList
            DiagnosisList.Add(f)
        Next
    End Sub
    Private Sub AddFindingsButton_Click(ByVal sender As Object, ByVal e As System.Windows.RoutedEventArgs) Handles AddFindingsButton.Click
        Dim win As New PatientFindingsWindow
        win.ShowDialog()
        If win.findingList Is Nothing Then Exit Sub
        For Each f In win.findingList
            findingList.Add(f)
        Next
    End Sub

    Private Sub AddProblemsButton_Click(ByVal sender As Object, ByVal e As System.Windows.RoutedEventArgs) Handles AddProblemsButton.Click
        Dim win As New PatientProblemsWindow
        win.ShowDialog()
        If win.ProblemList Is Nothing Then Exit Sub
        For Each f In win.ProblemList
            ProblemList.Add(f)
        Next
    End Sub

    Private Sub AddTreatmentButton_Click(ByVal sender As Object, ByVal e As System.Windows.RoutedEventArgs) Handles AddTreatmentButton.Click
        Dim win As New PatientTreatmentWindow
        win.ShowDialog()
        If win.PatientPrescriptionList Is Nothing Then Exit Sub
        For Each f In win.PatientPrescriptionList
            TreatmentList.Add(f)
        Next
    End Sub

    Private Sub PrintPrescriptionButton_Click(ByVal sender As Object, ByVal e As System.Windows.RoutedEventArgs) Handles PrintPrescriptionButton.Click
        Dim PrintForm As New ReportViewerWindow
        PrintForm.VisitID = Val(IDTextBlock.Text)
        PrintForm.PatientID = ID
        PrintForm.DrID = ConsultingDocComboBox.SelectedValue
        PrintForm.ReportType = ReportsList.PrescriptionReport
        PrintForm.ShowDialog()
    End Sub

    Private Sub PatientVisitUserControl_Loaded(ByVal sender As Object, ByVal e As System.Windows.RoutedEventArgs) Handles Me.Loaded
        Dim dataProvider As New MasterDataLayer()
        Dim lst As List(Of Lookups)

        lst = dataProvider.GetComplaintLookList()
        ChiefComplaintsComboBox.ItemsSource = lst

        lst = dataProvider.GetDoctorsLookList()
        ConsultingDocComboBox.ItemsSource = lst
    End Sub

    Private Sub VisitIDTextBlock_MouseLeftButtonDown(ByVal sender As System.Object, ByVal e As System.Windows.Input.MouseButtonEventArgs)
        Dim VisitID As Integer
        VisitID = Val(sender.text)
        Dim dataProvider As New PatientDataLayer()
        Dim visitObj As PatientVisit
        visitObj = dataProvider.GetPatientVisitCompleteInfo(ID, VisitID)
        If visitObj IsNot Nothing Then
            VisitDateDatePicker.SelectedDate = visitObj.visitDataTime
            ConsultingDocComboBox.SelectedValue = visitObj.consultingDoc
            ChiefComplaintsComboBox.SelectedValue = visitObj.compalinedComplaints
            NotesTextBox.Text = visitObj.visitNote
            ProblemList = visitObj.ProblemList
            findingList = visitObj.findingList
            DiagnosisList = visitObj.DiagnosisList
            TreatmentList = visitObj.TreatmentList
            ProblemsDataGrid.ItemsSource = ProblemList
            FindingsDataGrid.ItemsSource = findingList
            DiagnosisDataGrid.ItemsSource = DiagnosisList
            TreatmentsDataGrid.ItemsSource = TreatmentList
            PrintPrescriptionButton.IsEnabled = True
            IDTextBlock.Text = VisitID
        End If
    End Sub
End Class
